import { projectLonLat } from "../projection/Projection.LonLat";
import { toTransformation } from "../../geometry/Transformation";
import { Earth } from "./CRS.Earth";

export class EPSG4326class extends Earth {
  code = "EPSG:4326";
  projection = projectLonLat();
  transformation;

  constructor() {
    super();
    this.setTransformation();
  }

  setTransformation() {
    // const scale = 0.5 / (Math.PI * this.projection.R);
    // this.transformation = toTransformation(scale, 0.5, -scale, 0.5);
    this.transformation = toTransformation(1 / 180, 1, -1 / 180, 0.5);
    console.log("transformation", this.transformation);
  }
  // 经纬度转点
  latLngToPoint(latlng, zoom) {
    // 把经纬度转web墨卡托投影
    const projectedPoint = this.projection.project(latlng),
      scale = this.scale(zoom);
    // 墨卡托投影的原点在非洲旁边，把他偏移到瓦片原点
    return this.transformation._transform(projectedPoint, scale);
  }
  // 点转经纬度
  pointToLatLng(point, zoom) {
    const scale = this.scale(zoom),
      untransformedPoint = this.transformation.untransform(point, scale);

    return this.projection.unproject(untransformedPoint);
  }
}

export const EPSG4326 = () => {
  return new EPSG4326class();
};
